package org.example.controller;

import com.google.common.collect.Sets;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.entity.AuditContentParamDto;
import org.example.entity.SysConfig;
import org.example.enums.ConfirmedStateEnum;
import org.example.service.testSendMailService;
import org.example.utils.AESEncryptUtils;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;
import java.util.Set;

/**
 * @author weigang
 * @create 2022-07-08 18:02
 */

@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/send-mail")
public class testSendMailController {

    private testSendMailService testSendMailService;
    private final SysConfig sysConfig;


    @GetMapping("/commit-check")
    public void testSendMail(){
        testSendMailService.testSendCheckMail();
        testSendMailService.testSendCommitMail();
    }

    /**
     * 接受审核邮件，跳转审核接口
     *
     * @param request
     * @param auditFlag
     * @return Response
     */
    @GetMapping("/preAudit/{auditFlag}")
    public void preAudit(HttpServletRequest request, @PathVariable String auditFlag){

        AuditContentParamDto auditContentParam = new AuditContentParamDto();
        if (Objects.equals("success", auditFlag)) {
            auditContentParam.setConfirmState(ConfirmedStateEnum.CONFIRMED.getState());
        } else {
            auditContentParam.setConfirmState(ConfirmedStateEnum.CONFIRM_REJECTED.getState());
        }

        String userId = new String(Objects.requireNonNull(AESEncryptUtils.decrypt(AESEncryptUtils.parseHexStr2Byte(request.getParameter("cpi")), sysConfig.getAesKey())));
        String userName = new String(Objects.requireNonNull(AESEncryptUtils.decrypt(AESEncryptUtils.parseHexStr2Byte(request.getParameter("ic")), sysConfig.getAesKey())));

        Set<String> userIdsSet = Sets.newHashSet();
        userIdsSet.add(userId);
        auditContentParam.setUserIds(userIdsSet);
        // 接下来再进行相应处理
        // 调用service || 定时任务 || 发布事件
        log.info("调用接口成功，审核同意");

    }

    @GetMapping("/index")
    public void page(){
        log.info("back to index page");
    }

}
